package com.pu.controller.admin;

import com.pu.constant.JwtClaimsConstant;
import com.pu.dto.EmployeeDTO;
import com.pu.dto.EmployeeLoginDTO;
import com.pu.dto.EmployeePageQueryDTO;
import com.pu.dto.PasswordEditDTO;
import com.pu.entity.Employee;
import com.pu.properties.JwtProperties;
import com.pu.result.PageResult;
import com.pu.result.Result;
import com.pu.service.EmployeeService;
import com.pu.utils.JwtUtil;
import com.pu.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Yusc
 * @Date: 2023/10/22 17:10
 * @Version:
 * @Description: 员工管理
 **/
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关的接口")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * @auther: Yusc
     * @date: 2023/10/22 17:35
     * @Description: 登录
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * @auther: Yusc
     * @date: 2023/10/22 17:36
     * @Description: 登录退出
     */
    @ApiOperation("员工登录退出")
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request) {
        // 清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return Result.success("退出成功");
    }

    /**
     * @param passwordEditDTO
     * @auther: Yusc
     * @date 2023/11/5 18:01
     * @Description 修改密码
     */
    @PutMapping("/editPassword")
    @ApiOperation("修改密码")
    public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO) {
        log.info("修改密码：{}", passwordEditDTO);
        employeeService.editPassword(passwordEditDTO);
        return Result.success("密码修改成功!");
    }

    /**
     * @param employeeDTO
     * @auther: Yusc
     * @date: 2023/10/25 16:03
     * @Description:新增员工
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工：{}", employeeDTO);
        employeeService.save(employeeDTO);//该方法后续步骤会定义
        return Result.success();
    }

    /**
     * @param employeePageQueryDTO
     * @auther: Yusc
     * @date: 2023/10/25 17:16
     * @Description: 员工分页条件查询
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询，参数为：{}", employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @param status
     * @param id
     * @auther: Yusc
     * @date: 2023/10/25 18:38
     * @Description: 启用禁用员工账号
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用员工账号")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("启用禁用员工账号：status:{},id:{}", status, id);
        employeeService.startOrStop(status, id);
        return Result.success();
    }


    /**
     * @param id
     * @auther: Yusc
     * @date 2023/10/26 11:31
     * @Description 根据id查询修改员工信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<Employee> getById(@PathVariable Long id) {
        log.info("根据id编辑员工信息 id：{}:", id);
        Employee employee = employeeService.getById(id);
        employee.setPassword("******");
        return Result.success(employee);
    }

    /**
     * @param employeeDTO
     * @auther: Yusc
     * @date 2023/10/26 11:48
     * @Description 编辑员工信息
     */
    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("编辑员工信息：{}", employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }


}
